package com.ruoyi.process.pipemaintainence.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.PageConstants;
import com.ruoyi.common.entity.APIRequest;
import com.ruoyi.common.entity.APIResult;
import com.ruoyi.common.enums.StateEnum;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.process.pipemaintainence.service.BpmnApprovalService;
import com.ruoyi.process.pipemaintainence.service.BpmnCopyProcessService;
import com.ruoyi.process.pipemaintainence.service.IDrainagePipeMainService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Api(tags = "抄送管理", value = "抄送管理")
@RestController
@RequestMapping("/bpmn/receive")
public class ReceiverProcessController {

    protected static final Logger logger = LoggerFactory.getLogger(ReceiverProcessController.class);

    @Autowired
    protected BpmnCopyProcessService processService;

    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    protected IDrainagePipeMainService drainagePipeMainService;

    @Autowired
    protected ISysUserService userService;

    @Autowired
    protected HistoryService historyService;

    @Autowired
    protected BpmnApprovalService bpmnApprovalService;


    /**
     * 查询抄送的流程
     * @param request
     * @return
     */
    @ApiOperation(value = "通过用户id查询抄送的流程", notes = "通过用户id查询抄送的流程")
    @RequestMapping(value = "/findByUserId", method = RequestMethod.POST)
    public APIResult findByUserId(@RequestBody(required = true) APIRequest request) {
        APIResult result = new APIResult<>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        try {
            Integer pageSize = request.getRequestPage().getLimit();
            Integer pageNo = request.getRequestPage().getPageNo();
            if (pageNo == null) pageNo = PageConstants.DEFAULT_PAGE_NO;
            if (pageSize == null) pageSize = PageConstants.DEFAULT_PAGE_SIZE;
            //根据传递的参数userId查询当前登录人被抄送的流程
            String userId = request.getParameter("userId");
            SysUser sysUser = userService.selectUserById(Long.valueOf(userId));
            if (BeanUtils.isEmpty(sysUser)) throw new RuntimeException("用户不存在");
            //TODO
            if (BeanUtils.isEmpty(userId)) {
                //获取当前登录用户
                SysUser user = ShiroUtils.getSysUser();
                if (BeanUtils.isEmpty(user)) {
                    throw new RuntimeException("用户未登录，请登录再操作");
                }
            }
            listMap = processService.findCopyProcessByUserId(pageNo, pageSize, userId);
            PageInfo pageInfo = new PageInfo(listMap);
            result.setData(pageInfo);
            result.setMessage("查询抄送的流程成功");
        } catch (Exception e) {
            result.setState(StateEnum.ERROR_BPMN.getCode());
            result.setMessage(StateEnum.ERROR_BPMN.getText());
            result.setCause(e.getMessage());
            logger.error("/bpmn/receive/findByUserId", e);
        }
        return result;
    }


}
